//
//  UICollectionViewDemo.swift
//  firstProject
//
//  Created by 张彦辉 on 2017/3/24.
//  Copyright © 2017年 张彦辉. All rights reserved.
//

import UIKit

class UICollectionViewDemo: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        let rect = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        let layout = UICollectionViewFlowLayout()
        //设置collection 的宽高
//        layout.itemSize = CGSize(width: 80, height: 80)
        //设置列间距
        //layout.minimumInteritemSpacing = 15
        //设置行间距
        //layout.minimumLineSpacing = 30
        //设置上下左右的偏移量
        layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10)
        let collection = UICollectionView.init(frame: rect, collectionViewLayout: layout)
//        collection.backgroundColor = UIColor.white
        collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
        collection.dataSource = self
        collection.delegate = self
        self.view.addSubview(collection)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

// MARK: - UICollectionViewDataSource
extension UICollectionViewDemo{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 50
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("点击事件\(indexPath.row)")
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
        cell.backgroundColor = UIColor.green
        let label = UILabel()
        label.text = "\(indexPath.row)"
        label.backgroundColor = UIColor.red
        cell.backgroundView = label
        
        let label1 = UILabel()
        label1.text = "选中"
        label1.backgroundColor = UIColor.red
        cell.selectedBackgroundView = label1
        
        return cell
    }
}
